package com.example.wxback.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentTime() {
        return simpleDateFormat.format(new Date());
    }

    public static String getTimeFromDate(Date date) {
        return simpleDateFormat.format(date);
    }

    /**
     * 当前时间加减amount天
     *
     * @param amount
     * @return
     */
    public static Date plusDay(Date date, int amount) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, amount); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }

    /**
     * 当前时间加减月
     *
     * @param amount
     * @return
     */
    public static Date plusMonth(Date date, int amount) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, amount);
        return calendar.getTime();
    }

    /**
     * 当前时间加减年
     *
     * @param amount
     * @return
     */
    public static Date plusYear(Date date, int amount) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, amount);
        return calendar.getTime();
    }
}
